/* This file is part of Stimer. Stimer - Simple reminder tool.
    Copyright (C) 2012  Klimov Max <cleemmi@gmail.com>

    Stimer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    */

#include "remindercontroller.h"
#include <QObject>
#include <QSettings>
#include "reminder.h"
#include "reminderview.h"

ReminderController::ReminderController():model(0),view(0),tray(0)
{
}

ReminderController::ReminderController(Reminder *model, ReminderView *view, TrayView *tray):model(model),view(view),tray(tray)
{
    if (model&&view&&tray) connect();
}

const QTime& ReminderController::getTime()
{
    return model->getTime();
}

void ReminderController::setTime(const QTime &time)
{
    model->setTime(time);
}

QTime ReminderController::getElapsedTime()
{
    return QTime(0,0,0,0).addMSecs(model->getElapsedMSec());
}

QTime ReminderController::getStartTime()
{
    return model->getStartTime();
}

void ReminderController::start()
{
    model->start();
}

void ReminderController::stop()
{
    model->stop();
}

void ReminderController::saveSettings()
{
    QSettings settings("cleem", "Stimer");
    settings.beginGroup("Timer");
    settings.setValue("value",model->getTime());
    settings.endGroup();
}

void ReminderController::loadSettings()
{
    QSettings settings("cleem", "Stimer");
    settings.beginGroup("Timer");
    model->setTime(settings.value("value").value<QTime>());

    settings.endGroup();
}

bool ReminderController::connect()
{
    bool result = true;
    result = result&&(this->tray->connect(this->tray,SIGNAL(startClicked()),this->model,SLOT(start())));
    result = result&&(this->tray->connect(this->tray,SIGNAL(stopClicked()),this->model,SLOT(stop())));
    result = result&&(this->tray->connect(this->tray,SIGNAL(trayClicked()),this->view,SLOT(showOnCursor())));

    result = result&&(this->tray->connect(this->model,SIGNAL(reminded()),this->tray,SLOT(remind())));
    result = result&&(this->tray->connect(this->model,SIGNAL(started()),this->tray,SLOT(start())));
    result = result&&(this->tray->connect(this->model,SIGNAL(stoped()),this->tray,SLOT(stop())));
    result = result&&(this->tray->connect(this->model,SIGNAL(timeChanged(QTime)),this->view,SLOT(setTime(QTime))));

    result = result&&(this->tray->connect(this->view,SIGNAL(timeChanged(QTime)),this->model,SLOT(setTime(QTime))));
    return result;
}

bool ReminderController::disconnect()
{
    bool result = true;
    result = result&&(this->tray->disconnect(this->tray,SIGNAL(startClicked()),this->model,SLOT(start())));
    result = result&&(this->tray->disconnect(this->tray,SIGNAL(stopClicked()),this->model,SLOT(stop())));
    result = result&&(this->tray->disconnect(this->tray,SIGNAL(trayClicked()),this->view,SLOT(showOnCursor())));

    result = result&&(this->tray->disconnect(this->model,SIGNAL(reminded()),this->tray,SLOT(remind())));
    result = result&&(this->tray->disconnect(this->model,SIGNAL(started()),this->tray,SLOT(start())));
    result = result&&(this->tray->disconnect(this->model,SIGNAL(stoped()),this->tray,SLOT(stop())));
    result = result&&(this->tray->disconnect(this->model,SIGNAL(timeChanged(QTime)),this->view,SLOT(setTime(QTime))));

    result = result&&(this->tray->disconnect(this->view,SIGNAL(timeChanged(QTime)),this->model,SLOT(setTime(QTime))));
    return result;
}




